package com.kdtech.analyse.news;
import com.kdtech.analyse.AnalyseNews;
import com.kdtech.utils.HtmlCleaner;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;

import com.kdtech.analyse.JSoupUtils;
import com.kdtech.crawler.CrawlHTML;
import com.kdtech.crawler.at.UrlArgumentTop;
import com.kdtech.entity.crawler.UrlMeta;
import com.kdtech.entity.data.NewsMeta;
import com.kdtech.utils.DateUtils;
import com.kdtech.utils.StringUtils;
import com.kdtech.utils.HtmlCleaner;

/**
 * http://www.nit.net.cn/ 浙江大学宁波理工学院
 * @author Persh
 *
 */
public class NitNewsAnalyse implements AnalyseNews {
	
	private static final String[] regex={
		"http://[a-z]*.nit.net.cn/.*/[0-9]*.htm",
		"http://www.nit.zju.edu.cn/[a-zA-Z]*/[0-9]*.htm",
		"http://job.nit.net.cn/job/news.php\\?id=[0-9]*&tb=[0-9]*",
		"http://sce.nit.net.cn/html/[a-z]*/[0-9]*/[0-9]*/[0-9]*.html"
	};
	
	
	
	public boolean isDetailPage(String url) {
		for (int i=0; i < regex.length; i++) {
			if (url.matches(regex[i])) {
				return true;
			}
		}
		return false;
	}

	
	public NewsMeta parserHtml(UrlMeta urlMeta) {
		NewsMeta news=new NewsMeta();
		if (urlMeta.getHtml() == null) {
		}		
		String htmltxt=urlMeta.getHtml();
		String url=urlMeta.getUrl();		
				
		news.setUrl(url);		
		String title=null;
		String content=null;
		String author=null;
		Long date=null;
		Document doc=Jsoup.parse(htmltxt);
		title =doc.select("td.style_bt").text();
		if(StringUtils.isBlank(title)){
			title =doc.select("td.news_title2").text();
		}
		if(StringUtils.isBlank(title)){
			title =doc.select("td.style_blue14").text();
		}
		if(StringUtils.isBlank(title)){
			title =doc.select("td.bb").text();
		}
		if(StringUtils.isBlank(title)){
			title =doc.select("li.title").text();
		}
		if(StringUtils.isBlank(title)){
			title =doc.select("span#lbNewsTitle").text();
		}
		content=HtmlCleaner.getContentHtml(url,doc.select("td#content"));
		if(StringUtils.isBlank(content)){
			content=HtmlCleaner.getContentHtml(url,doc.select("td.content14"));
		}
		if(StringUtils.isBlank(content)){
			content=HtmlCleaner.getContentHtml(url,doc.select("td.page_title"));
		}
		if(StringUtils.isBlank(content)){
			content=HtmlCleaner.getContentHtml(url,doc.select("td.info_p"));
		}
		if(StringUtils.isBlank(content)){
			content=HtmlCleaner.getContentHtml(url,doc.select("li.content"));
		}
		if(StringUtils.isBlank(content)){
			content=HtmlCleaner.getContentHtml(url,doc.select("div.endText"));
		}
		date=DateUtils.matchDate(doc.select("td.content").text());
		if(date == null) {
			date=DateUtils.matchDate(doc.select("td.page_title").text());
		}
		if(date == null) {
			date=DateUtils.matchDate(doc.select("td[height=35]").text());
		}
		if(date == null) {
			date=DateUtils.matchDate(doc.select("li.title_fu").text());
		}
		if(date == null) {
			date=DateUtils.matchDate(doc.select("span#lbCreateTime").text());
		}
		author=JSoupUtils.matchAuthor(doc, "来源：");

		if(StringUtils.isBlank(title)){
		}
		news.setTitle(StringUtils.trimSpace(title));
		news.setContent(content);
		news.setAuthor(author);
		news.setDate(date);	
		
		return news;
	}
	
	public static void main(String[] args) {
		NitNewsAnalyse analyse=new NitNewsAnalyse();
		String url="http://sce.nit.net.cn/html/kstz/2015/0902/215.html";
		url = UrlArgumentTop.FromatUrl(url);
		System.out.println(UrlArgumentTop.FromatUrl(url));
		System.out.println(analyse.isDetailPage(url));
		UrlMeta meta=CrawlHTML.responseToURL(url);
		System.out.println(analyse.parserHtml(meta));
	}

	
	public NewsMeta Update(NewsMeta meta) {
		return null;
	}

	
	public boolean isNeedUpdate(){
		return false;
	}
}
